package com.rdc.example.dao.impl;

import com.rdc.example.entity.Follow;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import static com.rdc.example.util.DBUtil.closeResources;
import static com.rdc.example.util.DBUtil.getConnection;

/**
 * @author duck
 */
public class FollowDaoImpl implements com.rdc.example.dao.FollowDao {
    public static void main(String[] args) throws Exception {
        FollowDaoImpl followDao = new FollowDaoImpl();
        System.out.println(followDao.followCheck(1,1));
    }

    /**
     *
     * @param follow
     * @return
     * @throws Exception
     */
    @Override
    public boolean insert(Follow follow) throws Exception {
        // 进行数据库插入操作
        Connection conn =null;
        PreparedStatement pstmt=null;
        try {
            conn = getConnection();
            String sql ="INSERT INTO `follow` (follower_id,followed_id) VALUES (?,?)";
            pstmt = conn.prepareStatement(sql);
            insertFollow(pstmt, follow);
            int row = pstmt.executeUpdate();
            return row > 0;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(null, pstmt, conn);
        }
    }

    /**
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteById(Integer id) throws Exception {
        // 进行数据库删除操作
        Connection conn =null;
        PreparedStatement pstmt=null;
        try {
            conn = getConnection();
            String sql = "delete from `follow` where id=?" ;
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, id);
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLIntegrityConstraintViolationException e) {
            e.printStackTrace();
        }finally {
            closeResources(null,pstmt,conn);
        }
        return false;
    }

    /**
     *
     * @param followerId
     * @param followedId
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteByFollowerIdFollowedId(Integer followerId, Integer followedId) throws Exception {
        // 进行数据库删除操作
        Connection conn =null;
        PreparedStatement pstmt=null;
        try {
            conn = getConnection();
            String sql = "delete from `follow` where follower_id = ? and followed_id = ?" ;
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, followerId);
            pstmt.setLong(2, followedId);
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLIntegrityConstraintViolationException e) {
            e.printStackTrace();
        }finally {
            closeResources(null,pstmt,conn);
        }
        return false;
    }

    /**
     *
     * @param followerId
     * @param followedId
     * @return
     * @throws Exception
     */
    @Override
    public boolean followCheck(Integer followerId, Integer followedId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "select count(*) from `follow` where follower_id = ? and followed_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, followerId);
            pstmt.setInt(2, followedId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(rs, pstmt, conn);
        }
    }


    /**
     *
     * @param followedId
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteByFollowedId(Integer followedId) throws Exception {
        // 进行数据库删除操作
        Connection conn =null;
        PreparedStatement pstmt=null;
        try {
            conn = getConnection();
            String sql = "delete from `follow` where follow_id=?" ;
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, followedId);
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLIntegrityConstraintViolationException e) {
            e.printStackTrace();
        }finally {
            closeResources(null,pstmt,conn);
        }
        return false;
    }

    /**
     *
     * @param followerId
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> listFollowedIdsByFollowerId(Integer followerId) throws Exception {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Integer> followedIds = new ArrayList<>();
        try {
            String sql = "SELECT followed_id from follow where follower_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, followerId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Integer id = rs.getInt("followed_id");
                followedIds.add(id);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeResources(rs, pstmt, conn);
        }
        return followedIds;
    }

    /**
     *
     * @param followedId
     * @return
     * @throws Exception
     */
    @Override
    public List<Follow> listFollowByFollowedId(Integer followedId) throws Exception {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Follow> followList = new ArrayList<>();
        try {
            String sql = "SELECT * from follow where followed_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, followedId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Follow follow = getFollow(rs);
                followList.add(follow);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeResources(rs, pstmt, conn);
        }
        return followList;
    }






    private void insertFollow(PreparedStatement pstmt, Follow follow) throws Exception {
        pstmt.setInt(1, follow.getFollowerId());
        pstmt.setInt(2, follow.getFollowedId());
    }

    private Follow getFollow(ResultSet rs) throws Exception {
        Follow follow = new Follow();
        follow.setId(rs.getInt("id"));
        follow.setFollowerId(rs.getInt("follower_id"));
        follow.setFollowedId(rs.getInt("followed_id"));
        follow.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
        return follow;
    }
}
